package com.platform.usercenter.controller;


import com.platform.common.core.utils.poi.ExcelUtil;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.core.web.page.TableDataInfo;
import com.platform.usercenter.domain.CalTeam;
import com.platform.usercenter.service.ICalTeamMemberService;
import com.platform.usercenter.service.ICalTeamService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 班组Controller
 *
 * @author yinjinlu
 * @date 2022-06-05
 */
@RestController
@RequestMapping("/mes/cal/team")
public class CalTeamController extends BaseController {
    @Resource
    private ICalTeamService calTeamService;

    @Resource
    private ICalTeamMemberService calTeamMemberService;

    /**
     * 查询班组列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CalTeam calTeam) {
        startPage();
        List<CalTeam> list = calTeamService.selectCalTeamList(calTeam);
        return getDataTable(list);
    }

    /**
     * 查询所有班组列表
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(CalTeam calTeam) {
        List<CalTeam> list = calTeamService.selectCalTeamList(calTeam);
        return AjaxResult.success(list);
    }

    /**
     * 导出班组列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CalTeam calTeam) {
        List<CalTeam> list = calTeamService.selectCalTeamList(calTeam);
        ExcelUtil<CalTeam> util = new ExcelUtil<CalTeam>(CalTeam.class);
        util.exportExcel(response, list, "班组数据");
    }

    /**
     * 获取班组详细信息
     */
    @GetMapping(value = "/{teamId}")
    public AjaxResult getInfo(@PathVariable("teamId") Long teamId) {
        return AjaxResult.success(calTeamService.selectCalTeamByTeamId(teamId));
    }

    /**
     * 新增班组
     */
    @PostMapping
    public AjaxResult add(@RequestBody CalTeam calTeam) {
        return toAjax(calTeamService.insertCalTeam(calTeam));
    }

    /**
     * 修改班组
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CalTeam calTeam) {
        return toAjax(calTeamService.updateCalTeam(calTeam));
    }

    /**
     * 删除班组
     */
    @Transactional
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        for (Long teamId : teamIds
        ) {
            calTeamMemberService.deleteByTeamId(teamId);
        }
        return toAjax(calTeamService.deleteCalTeamByTeamIds(teamIds));
    }
}
